package mlz.a_polymorphic.demo02;

public class DemoTest02 {
    public static void main(String[] args) {
        Animal d = new Dog();
        d.eat();
        Animal c = new Cat();
        c.eat();


        //错误语法
        //ClassCastException: 类型转换异常
//        Dog dog = (Dog) c;
//        dog.watchHouse();

        //instanceof关键词 在转型之前进行判断
        if (c instanceof Dog){
            Dog dog = (Dog) c;
            dog.watchHouse();
        } else if (c instanceof Cat){
            Cat cat = (Cat) c;
            cat.catchMouse();
        }


    }
}
